//https://www.nowcoder.com/practice/9b4c81a02cd34f76be2659fa0d54342a?tpId=13&tqId=23279&ru=/exam/oj/ta&qru=/ta/coding-interviews/question-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D13%26type%3D13
//https://leetcode.cn/problems/spiral-matrix/
#include <iostream>
#include <vector>
#include <climits>
#include <stack>
#include <queue>

using namespace std;

class Solution {
  public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        if (matrix.empty()) return {};
        if (matrix[0].empty()) return {};


        int columns = matrix[0].size() - 1;
        int rows = matrix.size() - 1;

        int current_colum = 0;
        int current_row = 0;
        std::vector<int > t_vec;
        while (current_row <= rows && current_colum <= columns) {
            InsertValue(t_vec, current_row++, current_colum++, rows--, columns--, matrix);
        }
        return  t_vec;

    }

    void InsertValue(std::vector<int >& t_vec, int row1, int col1, int row2,
                     int col2, vector<vector<int> >& matrix) {
        if (row1 == row2) {
            for (int i = col1; i <= col2; ++i) {
                t_vec.push_back(matrix[row1][i]);
            }
        } else if (col1 == col2) {
            for (int i = row1; i <= row2; ++i) {
                t_vec.push_back(matrix[i][col1]);
            }
        } else {
            //记录当前的位置
            int curC = col1;
            int curR = row1;
            while (curC != col2) {
                t_vec.push_back(matrix[row1][curC]);
                ++curC;
            }
            while (curR != row2) {
                t_vec.push_back(matrix[curR][col2]);
                ++curR;
            }
            while (curC != col1) {
                t_vec.push_back(matrix[row2][curC]);
                --curC;
            }
            while (curR != row1) {
                t_vec.push_back(matrix[curR][col1]);
                --curR;
            }
        }

    }
};